package com.yoomiito.app.ui.anyview.goodsinfo.bottom

import android.graphics.drawable.GradientDrawable
import android.view.View
import android.widget.LinearLayout
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.isEmptyOr0
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.removeDecimalPoint
import com.yoomiito.app.ui.goods.NewGoodsInfoActivity
import com.yoomiito.app.utils.AllEventID
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.LoginUtils
import com.yoomiito.app.utils.RouterUtils
import com.yoomiito.app.utils.TouchUtils
import com.yoomiito.app.utils.Type
import com.yoomiito.app.utils.UserInfoConfig
import com.yoomiito.app.utils.YMToast
import com.yoomiito.app.widget.dialog.PayDialog
import com.yoomiito.app.widget.dialog.ShopCarDialog
import kotlinx.android.synthetic.main.view_goodsinfo_bottombar.view.collectionSv
import kotlinx.android.synthetic.main.view_goodsinfo_bottombar.view.goHome
import kotlinx.android.synthetic.main.view_goodsinfo_bottombar.view.goodsCollection
import kotlinx.android.synthetic.main.view_goodsinfo_bottombar.view.leftBtn
import kotlinx.android.synthetic.main.view_goodsinfo_bottombar.view.leftBtnTopTv
import kotlinx.android.synthetic.main.view_goodsinfo_bottombar.view.rightBtn
import kotlinx.android.synthetic.main.view_goodsinfo_bottombar.view.rightBtnTopTv

class NewTaoBaoBottomBar(context: BaseActivity<*>) : BaseBottomBar(context) {

    override fun getLayoutId(): Int = R.layout.view_goodsinfo_bottombar

    override fun getParentView(): LinearLayout = this

    override fun initView(goodsDetail: GoodsDetail) {
        goHome.visibility = View.GONE
        goodsCollection.visibility = View.VISIBLE
        val drawable = leftBtn.background as GradientDrawable
        val radius = DensityUtil.dp2px(24f).toFloat()
        drawable.cornerRadius = radius
        val drawable1 = rightBtn.background as GradientDrawable
        drawable1.cornerRadius = radius
        collectionSv.setSelect(goodsDetail.is_collection == 1)

        goodsCollection.setOnClickListener {
            if (App.mAppConfig.isLogin) {
                collectGoods(goodsDetail.num_iid.toString())
            } else {
//                AllUtils.toLogin(context as NewGoodsInfoActivity, AllCode.LoginIn)
                LoginUtils.toOneKeyLogin(context)
            }
        }

        leftBtn.setOnClickListener {

        }

        rightBtn.setOnClickListener {
            if (App.mAppConfig.isLogin) {
                if (goodsDetail.coupon_price.isEmptyOr0()) {//立即购买
                    RouterUtils.TaoBaoWithUrl(goodsDetail.item_url)
                } else {
                    RouterUtils.TaoBaoWithUrl(goodsDetail.coupon_click_url)
                }
            } else {
                LoginUtils.toOneKeyLogin(context)
            }
            TouchUtils.onEvent(AllEventID.TaoBaoBuyEventId, UserInfoConfig.getRoleId(), UserInfoConfig.getAgent())
        }

        val money = goodsDetail.get_money.removeDecimalPoint()
//        leftBtnBottomTv.visibility = if (!App.mAppConfig.isLogin || UserInfoConfig.getRoleId() < Type.LevelType.VIP_7 || goodsDetail.get_money.isEmptyOr0()) View.GONE else View.VISIBLE
//        leftBtnBottomTv.text = "赚¥${money}元"
        leftBtnTopTv.text = if (!App.mAppConfig.isLogin || UserInfoConfig.getRoleId() < Type.LevelType.VIP_7) "分享" else "分享赚钱"
//        rightBtnBottomTv.visibility = if (!App.mAppConfig.isLogin || goodsDetail.get_money.isEmptyOr0()) View.GONE else View.VISIBLE
//        if (!App.mAppConfig.isLogin or (UserInfoConfig.getRoleId() < Type.LevelType.VIP_7)){
//            if (SpannableUtils.isEmptyOr0(goodsDetail.coupon_price)){
//                rightBtnBottomTv.visibility = View.GONE
//            }else{
//                rightBtnBottomTv.visibility = View.VISIBLE
//                rightBtnBottomTv.text = "省${goodsDetail.coupon_price.removeDecimalPoint()}元"
//            }
//        }else{
//            if (goodsDetail.coupon_price.isEmptyOr0()) {
//                rightBtnBottomTv.text = "省${money.removeTrim()}元"
//            }else{
//                rightBtnBottomTv.text = "省${money.toBigDecimal().add(goodsDetail.coupon_price.toBigDecimal()).setScale(2, BigDecimal.ROUND_HALF_UP).toString()}元"
//            }
//        }
        rightBtnTopTv.text = goodsDetail.typeStr


        if(goodsDetail.buy == 2){
            rightBtn.isEnabled = false
            rightBtn.setBackgroundResource(R.drawable.bg_tv_2)
        }else{
            rightBtn.isEnabled = true
            rightBtn.setBackgroundResource(R.drawable.bg_tv_1)

        }

    }

    private fun collectGoods(id: String) {
        (context as NewGoodsInfoActivity).showLoading()
        if (collectionSv.isSelected) {
            delCollection(id)
        } else {
            addCollection(id)
        }
    }

    /**
     * 添加收藏
     *
     * @param id
     */
    private fun addCollection(id: String) {
        YmApi.getYmService()
                .addCollection(id)
                .compose((context as NewGoodsInfoActivity).bindToLifecycle())
                .compose(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose(XApi.getScheduler<BaseModel<*>>())
                .subscribe(object : ApiSubscriber<BaseModel<*>>() {
                    override fun onSuccess(baseModel: BaseModel<*>) {
                        YMToast.showText("收藏成功")
                        collectionSv.setSelect(true)
                        (context).dismissLoading()

                    }

                    override fun onFail(error: ApiException) {
                        YMToast.showText(error.msg)
                        (context).dismissLoading()

                    }
                })
    }

    /**
     * 取消收藏
     *
     * @param id
     */
    private fun delCollection(id: String) {
        YmApi.getYmService()
                .delCollection(id)
                .compose(context.bindToLifecycle())
                .compose(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose(XApi.getScheduler<BaseModel<*>>())
                .subscribe(object : ApiSubscriber<BaseModel<*>>() {
                    override fun onSuccess(baseModel: BaseModel<*>) {
                        YMToast.showText("已取消收藏")
                        collectionSv.setSelect(false)
                        (context).dismissLoading()

                    }

                    override fun onFail(error: ApiException) {
                        YMToast.showText(error.msg)
                        (context).dismissLoading()

                    }
                })
    }

}